package data

import (
	"fmt"
	"github.com/go-redis/redis/v7"
	"hotMoney/config"
)


var client *redis.Client

func InitRedis(conf config.AppConfig) {
	client = redis.NewClient(&redis.Options{
		Addr:     conf.App.Redis.Address,
		Password: conf.App.Redis.Password, // It's ok if password is "".
		DB:       0,  // use default DB
	})
	code := client.Ping()
	fmt.Println("the code is: ", code)
}


//未消费积分队列
func SetNRewardList(key string, id string)(int64, error) {
	return client.LPush(key, id).Result()
}


//积分info队列
func SetRewardInfo(key string, id, money string)(int64, error) {
	return client.HSet(key, id, money).Result()
}


//执行抢积分的lua脚本
func EvalSHA(sha string, args []string)(interface{}, error) {
	val, err := client.Eval(sha, args).Result()
	if err != nil {
		fmt.Println("执行积分的lua脚本失败，请检查！")
		return nil, err
	}
	return val, err
}


//令牌桶
func FilterEval(sha string, args []string)(interface{}, error) {
	val, err := client.Eval(sha, args).Result()
	fmt.Println("the filter_res is: ", val)
	if err != nil {
		fmt.Println("执行filter的lua脚本失败，请检查！")
		return nil, err
	}
	return val, err
}



func GetAmount(id string)(string, error) {
	amount, err:= client.HGet("rewardInfo", id).Result()
	return amount, err
}
